import request from '@/utils/request'

type Obj = Record<string, any>

// 查询列表
export const listINFO = (query:Obj, sort:string) => {
  let url = ''
  if(sort === '默认'){
    url = '/system/goldVs/lightAlarmCompList'
  } else if(sort === '升序'){
    url = '/system/goldVs/lightAlarmCompAscList'
  } else if(sort === '降序'){
    url = '/system/goldVs/lightAlarmCompDescList'
  }

  return request({
    url,
    method: 'get',
    params: query
  })
}

export const _trendsTitle:string[] = ['小报警率(%)', '小报警数(件)', '小报警时(分)']

const cellStyle = ({ rowIndex, data, value }: Obj) => {
  if(rowIndex === 0){
    if(_trendsTitle.includes(value)){
      if(value === '小报警率(%)'){
        return {
          background: '#fce4d6',
          color: 'blue',
          cursor: 'pointer',
          fontWeight: 'bold',
          textDecoration: 'underline'
        }
      }
      return {
        background: '#fce4d6'
      }
    } else {
      return {
        color: 'blue',
        cursor: 'pointer',
        fontWeight: 'bold',
        textDecoration: 'underline'
      }
    }
  }

  if(data.eqpAttribute1 === '实绩-目标' && Number(value) < 0){
    return {
      color: 'red',
      fontWeight: 'bold'
    }
  }

}

export const columnsDiewa = [
  { headerName: "eqpAttribute1", field: "eqpAttribute1", minWidth: 120, cellStyle, tooltipField:'eqpAttribute1' },
  { headerName: "eqpAttribute2", field: "eqpAttribute2", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute3", field: "eqpAttribute3", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute4", field: "eqpAttribute4", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute5", field: "eqpAttribute5", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute6", field: "eqpAttribute6", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute7", field: "eqpAttribute7", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute8", field: "eqpAttribute8", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute9", field: "eqpAttribute9", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute10", field: "eqpAttribute10", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute11", field: "eqpAttribute11", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute12", field: "eqpAttribute12", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute13", field: "eqpAttribute13", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute14", field: "eqpAttribute14", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute15", field: "eqpAttribute15", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute16", field: "eqpAttribute16", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute17", field: "eqpAttribute17", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute18", field: "eqpAttribute18", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute19", field: "eqpAttribute19", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute20", field: "eqpAttribute20", minWidth: 45, cellStyle },
  { headerName: "eqpAttribute21", field: "eqpAttribute21", minWidth: 45, cellStyle }
]

export const getTableData = (data:any[]) => {
  let _tempObj:Obj = {
    "小报警率": '小报警率(%)',
    "小报警数": '小报警数(件)',
    "小报警时": '小报警时(分)'
  }
  data.forEach((val:any) => {
    val.goldVsReturnList.forEach((item:any) => {
      if(item.eqpAttribute1 === '设备'){
        for (let key in item) {
          if(item[key]){
            item[key] = item[key].includes('H05') ? item[key].slice(4) : item[key]
          }
        }
      }

      if(item.eqpAttribute1 === '设备'){
        item.eqpAttribute1 = _tempObj[val.type]
      } else if(item.eqpAttribute1.includes('差异')){
        item.eqpAttribute1 = '实绩-目标'
      } else if(item.eqpAttribute1.includes('_')){
        item.eqpAttribute1 = item.eqpAttribute1.split('_')[1]
      } else if(item.eqpAttribute1.includes(')')){
        item.eqpAttribute1 = item.eqpAttribute1.split(')')[1]
      }
    })
  })

  return data
}

export const chartOptios = (data:any[], idx: number, machine:string, time:string, sortable?:string) => {
  let legendData:string[] = ['实绩-目标']
  let xAxisData:any[] = machine === 'MBB' ? Object.values(data[0].goldVsReturnList[0]).slice(1) : (Object.values(data[0].goldVsReturnList[0]).slice(1)).slice(0, 20)
  let _dataPool:Obj = {}

  let _barData = ['激光', '印刷', '叠片']

  let _tempxAxisData = ['eqpAttribute2', 'eqpAttribute3', 'eqpAttribute4', 'eqpAttribute5', 'eqpAttribute6',
                        'eqpAttribute7', 'eqpAttribute8', 'eqpAttribute9', 'eqpAttribute10', 'eqpAttribute11',
                        'eqpAttribute12', 'eqpAttribute13', 'eqpAttribute14', 'eqpAttribute15', 'eqpAttribute16',
                        'eqpAttribute17', 'eqpAttribute18', 'eqpAttribute19', 'eqpAttribute20', 'eqpAttribute21']

  let _tempxAxisDataMBB = [..._tempxAxisData, 'eqpAttribute22', 'eqpAttribute23']

  let _lineData = ['目标', '实绩']

  let options:any = {
    title: {
      text: '',
      textStyle: {
        fontSize:'16px',
        fontWeight: 500
      }
    },
    legend: {
      right: '30',
      data: legendData
    },
    toolbox: {
      feature: {
        saveAsImage: {}, // 保存图片
        brush: {
          type: ['']
        }
      }
    },
    grid: [
      {
        left: '120px',
        right: '4px',
        height: '14%',
        top:'18%'
      },
      {
        left: '120px',
        right: '4px',
        top: '40%',
        // height: '50%',
        bottom: '10px'
      }
    ],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      borderWidth: 1,
      borderColor: '#ccc',
      padding: 10,
      textStyle: {
        color: '#000',
        fontSize: 12
      }
    },
    // color: _colors,
    xAxis: [
      {
        type: 'category',
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false // 不显示坐标轴线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        },
        data: xAxisData // 横坐标的数据
      },
      {
        gridIndex: 1,
        type: 'category',
        data: xAxisData,
        boundaryGap: true,
        axisLine: { onZero: false },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        }
        // min: 'dataMin',
        // max: 'dataMax'
      }
    ],
    yAxis: [
      {
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
        // min: 0
      },
      {
        gridIndex: 1,
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
      }
    ],
    series: []
  }

  data[idx].goldVsReturnList.forEach((item:Obj, idx:number) => {

    if(idx === 0){
      options.title.text = machine + '_' + item.eqpAttribute1 + '_' + time
    }

    if(idx !== 0){
      _dataPool[item.eqpAttribute1] = _tempxAxisData.map(key => item[key])
    }

    // 获取 图例
    if(_barData.includes(item.eqpAttribute1) || _lineData.includes(item.eqpAttribute1)){
      legendData.push(item.eqpAttribute1)
    }
  })

  for(let key in _dataPool) {

    if(_barData.includes(key)){
      options.series.push({
        name: key,
        type: 'bar',
        stack: '总量',
        barWidth: '40%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        // label: {
        //   show: true,
        //   position: 'insideBottom'
        // },
        data: _dataPool[key]
      })
    }

    if(_lineData.includes(key)){
      options.series.push({
        name: key,
        type: 'line',
        smooth: true,
        label: {
          show: key === '目标' ? false : true,
          position: 'top'
        },
        lineStyle: {
          width: 1
        },
        symbolSize: 2,
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: _dataPool[key]
      })
    }

    if(key === '实绩-目标'){
      options.series.push({
        name: key,
        type: 'bar',
        barWidth: '40%',
        label: {
          show: true,
          position: 'top'
        },
        itemStyle: {
          color: '#dbdbdb'
        },
        data: _dataPool[key]
      })
    }
  }

  return options
}